﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BarCode.AccessDao;
using BarCode.Common;
using BarCode.Bean;
using System.Text.RegularExpressions;
using LackCodePrint.Module;
using LackCodePrint.Common;

namespace LackCodePrint
{
    public partial class frmPackLeave : Form
    {

        private List<PackLeaveInfo> lstPrintInfo { set; get; }

        public List<PackLeaveInfo> lstLeaveInfo { set; get; }
        public List<PackLeaveInfo> lstUnQualifiedInfo { set; get; }
        public string startCode { set; get; }
        public CustomerInfo selectedCustomer { set; get; } // 选择的客户
        public string extra { set; get; } // 附加字符
        public int intDist { set; get; } // 微调距离


        private int codeLength = 0;
        private int totalCnt = 0;

        public frmPackLeave()
        {
            InitializeComponent();
        }

        private void frmPackLeave_Load(object sender, EventArgs e)
        {
            gridLeave.DataSource = lstLeaveInfo;
            gridUnQualified.DataSource = lstUnQualifiedInfo;
            codeLength = startCode.Length;
            
        }

        private void btnPrintLeave_Click(object sender, EventArgs e)
        {
            lstPrintInfo = lstLeaveInfo;
            totalCnt = lstPrintInfo.Count;
            DoPrint();
        }

        private void btnPrintUnQualified_Click(object sender, EventArgs e)
        {
            lstPrintInfo = lstUnQualifiedInfo;
            totalCnt = lstPrintInfo.Count;
            DoPrint();
        }

        public int getErrorInformation(int error_code, out string message)
        {
            int result = 0;
            byte[] errorInfo = new byte[1024];
            PrintLab.PTK_GetErrorInfo(error_code, errorInfo, errorInfo.Length);
            message = Encoding.Default.GetString(errorInfo).Trim('\0');
            return result;
        }

        private byte[] E_UTF8(string data)
        {
            return Encoding.UTF8.GetBytes(data);
        }

        private void showError(string FunctionName, int error_code)
        {
            PrintLab.PTK_CloseUSBPort();
            var message = "";
            int result = getErrorInformation(error_code, out message);
            MessageBox.Show(FunctionName + ":" + message);
        }

        private void DoPrint()
        {
            // 开始打印
            int totalCnt = lstPrintInfo.Count;
            int customerId = selectedCustomer.Id;
            for (int i = 0; i < totalCnt; i = i + 2)
                {
                  PrintInfo printInfo = new PrintInfo();
                  printInfo.intDist = intDist;
                  if (i == totalCnt - 1)
                  {
                    printInfo.strCode = new string[] { lstPrintInfo[i].leaveBH, "" };
                    //printInfo.strCode = new string[] { "123456789123312323", "" };
                }
                  else
                  {
                    printInfo.strCode = new string[] { lstPrintInfo[i].leaveBH, lstPrintInfo[i + 1].leaveBH };
                    //printInfo.strCode = new string[] { "12345678912312323", "32145678912312323" };
                }
                Console.WriteLine(lstPrintInfo[i].leaveBH.Length);
                  if (lstPrintInfo[i].leaveBH.Length == 17)
                  {
                      printInfo.narrow = "4";
                      printInfo.wide = "4";
                      Console.WriteLine("长度17");
                      NewMethod(printInfo.strCode, i, 1.5, 5.7, 13.5, 13.8, 33, 45, 2, 7, 4, 13, 14.5);
                    //BarCodePrint.PrtBarCode1AS17(printInfo);
                }
                  else if(lstPrintInfo[i].leaveBH.Length == 15)
                    {
                      printInfo.narrow = "5";
                      printInfo.wide = "5";
                      Console.WriteLine("长度15");
                      NewMethod(printInfo.strCode, i, 2.5, 5.7, 13.5, 13.8, 34, 45, 2, 7, 4, 14, 14.5);
                    //BarCodePrint.PrtBarCode1AS10(printInfo);
                }
                else if (lstPrintInfo[i].leaveBH.Length == 12)
                {
                    printInfo.narrow = "5";
                    printInfo.wide = "5";
                    Console.WriteLine("长度12");
                    NewMethod(printInfo.strCode, i, 1.2, 5.7, 13.5, 13.8, 32.5, 45, 3, 7, 4, 20, 14.5);
                    //BarCodePrint.PrtBarCode1AS10(printInfo);
                }
                else if (lstPrintInfo[i].leaveBH.Length == 13)
                {
                    printInfo.narrow = "5";
                    printInfo.wide = "5";
                    Console.WriteLine("长度13");
                    //NewMethod(printInfo.strCode, i, 1.5, 0.9, 13.5, 9, 33.5, 45.5, 2, 7, 4, 14);
                    NewMethod(printInfo.strCode, i, 3.5, 5.7, 14, 13.8, 34.5, 45, 2, 7, 4, (int)15.5, 14.5);
                    //BarCodePrint.PrtBarCode1AS10(printInfo);
                }
                else
                {
                    Console.WriteLine("长度随机");
                    printInfo.narrow = "5";
                    printInfo.wide = "5";
                    NewMethod(printInfo.strCode, i, 0.3, 5.7, 12.5, 13.8, 33.5, 46, 2, 7, 4, 20, 14.5);
                    //BarCodePrint.PrtBarCode1AS10(printInfo);
                }
            }
            /*else
            {
                for (int i = 0; i < 5; i = i + 2)
                {
                    PrintInfo printInfo = new PrintInfo();
                    printInfo.intDist = intDist;
                    if (i == totalCnt - 1)
                    {
                        printInfo.strCode = new string[] { lstPrintInfo[i].leaveBH, "" };
                    }
                    else
                    {
                        printInfo.strCode = new string[] { lstPrintInfo[i].leaveBH, lstPrintInfo[i + 1].leaveBH };
                    }
                    if (selectedCustomer.printMode == Const.PRINT_MODE_TWO_LONG)
                    {
                        printInfo.narrow = "4";
                        printInfo.wide = "4";
                        printInfo.extral = extra;
                        NewMethod(printInfo.strCode, i, 0.3, 2.5, 12.5, 7.8, 33.5, 46);
                        BarCodePrint.PrtBarCode2L(printInfo);

                    }
                    else
                    {
                        if (selectedCustomer.Id == 6)
                        {
                            printInfo.extral = "A";
                        }

                        if (selectedCustomer.Id == 2 || selectedCustomer.Id == 4 || selectedCustomer.Id == 8)
                        {
                            printInfo.narrow = "2";
                            printInfo.wide = "2";
                            NewMethod(printInfo.strCode, i, 0.3, 2.5, 12.5, 7.8, 33.5, 46);
                            BarCodePrint.PrtBarCode2SWithChar(printInfo);
                        }
                        else if (customerId == 1)
                        {
                            //是否包含字符
                            if (Regex.IsMatch(lstPrintInfo[i].leaveBH, @"[A-Za-z]{1}"))
                            {
                              //  有一个字母
                            printInfo.narrow = "2";
                                printInfo.wide = "2";
                                BarCodePrint.PrtBarCode2SWithChar(printInfo);
                                NewMethod(printInfo.strCode, i, 0.3, 2.5, 12.5, 7.8, 33.5, 46);
                            }
                            else
                            {
                                NewMethod(printInfo.strCode, i, 2.5, 3, 13, 8.3, 36, 46.5);
                                BarCodePrint.PrtBarCode2S(printInfo);
                            }

                        }
                        else if (customerId == 9)
                        {
                            printInfo.narrow = "2";
                            printInfo.wide = "2";
                            NewMethod(printInfo.strCode, i, 0.3, 2.5, 12.5, 7.8, 33.5, 46);
                            BarCodePrint.PrtBarCode2SL13(printInfo);
                        }
                        else
                        {
                            NewMethod(printInfo.strCode, i, 0.3, 2.5, 12.5, 7.8, 33.5, 46);
                            BarCodePrint.PrtBarCode2S(printInfo);
                        }
                        Console.WriteLine(printInfo.strCode[0] + "  " + printInfo.strCode[1]);

                    }
                }
            }*/
        }
        private void NewMethod(string [] lstPrintBarCode, int i, double xOne, double yOne, double xOneF, double yOneF, double xTwo, double xTwoF,int widthK,int widthZ ,int sizeHeight ,int sizeWidth,double height)
        {
            uint mm = 12;
            int result = 0;
            var message = "";
            /*连接打印机*/
            result = PrintLab.PTK_OpenUSBPort(255);
            if (result != 0)
            {
                result = getErrorInformation(result, out message);
                MessageBox.Show(message);
                return;
            }
            /*函数返回值*/
            int result_printing = 0;
            /*横坐标偏移,设置标签整体横向移动位置*/
            int offset_X = 60;
            /*纵坐标偏移，设置标签整体纵向移动位置*/
            int offset_Y = 0;
            /*清空缓存*/
            result_printing = PrintLab.PTK_ClearBuffer();
            if (result_printing != 0) { showError("PTK_ClearBuffer", result_printing); return; }
            /*设置打印黑度 取值范围 0-20*/
            result_printing = PrintLab.PTK_SetDarkness(10);
            if (result_printing != 0) { showError("PTK_SetDarkness", result_printing); return; }
            /*设置打印速度*/
            result_printing = PrintLab.PTK_SetPrintSpeed(4);
            if (result_printing != 0) { showError("PTK_SetPrintSpeed", result_printing); return; }
            /*设置打印方向*/
            result_printing = PrintLab.PTK_SetDirection('B');
            if (result_printing != 0) { showError("PTK_SetDirection", result_printing); return; }
            /*设置标签高度、间隙及偏移*/
            result_printing = PrintLab.PTK_SetLabelHeight((uint)(height * mm), 0, 2 * mm, false);
            if (result_printing != 0) { showError("PTK_SetLabelHeight", result_printing); return; }
            /*设置标签宽度，一定要准确，否则会导致打印内容位置不准确*/
            result_printing = PrintLab.PTK_SetLabelWidth(60 * mm);
            if (result_printing != 0) { showError("PTK_SetLabelWidth", result_printing); return; }
            /*打印一维码 此示例为Code128码*/
            result_printing = PrintLab.PTK_DrawBarcode((uint)(xOne * mm), (uint)(yOne * mm + offset_Y), 0, "1", (uint)widthK, (uint)widthZ, 6 * mm, 'N', E_UTF8(lstPrintBarCode[0]));//打印一维码
            if (result_printing != 0) { showError("PTK_DrawBarcode", result_printing); return; }
            /* 打印条码下方文字*/
            result_printing = PrintLab.PTK_DrawText_TrueType((uint)(xOneF * mm), (uint)(yOneF * mm), 50, (uint)sizeWidth, E_UTF8("微软雅黑"), 5, 400, false, false, false, E_UTF8(lstPrintBarCode[0]));
            if (result_printing != 0) { showError("PTK_DrawText_TrueType", result_printing); return; }
            if (lstPrintBarCode.Length == 2)
            {
                result_printing = PrintLab.PTK_DrawBarcode((uint)(xTwo * mm), (uint)(yOne * mm + offset_Y), 0, "1", (uint)widthK, (uint)widthZ, 6 * mm, 'N', E_UTF8(lstPrintBarCode[1]));//打印一维码
                if (result_printing != 0) { showError("PTK_DrawBarcode", result_printing); return; }
                /* 打印条码下方文字*/
                result_printing = PrintLab.PTK_DrawText_TrueType((uint)(xTwoF * mm), (uint)(yOneF * mm), 50, (uint)sizeWidth, E_UTF8("微软雅黑"), 5, 400, false, false, false, E_UTF8(lstPrintBarCode[1]));
                if (result_printing != 0) { showError("PTK_DrawText_TrueType", result_printing); return; }
            }
            /*打印，必须要执行这一行，否则不会打印，打印多张标签时每张标签后面都需要加它*/
            result_printing = PrintLab.PTK_PrintLabel(1, 1);
            if (result_printing != 0) { showError("PTK_PrintLabel", result_printing); return; }
            /*断开打印机连接*/
            PrintLab.PTK_CloseUSBPort();
        }

        //private void DoPrint()
        //{
        //    int printMode = 0;
        //    bool hasChar = false;
        //    startCode = startCode.ToUpper();
        //    if (Regex.IsMatch(startCode, @"^\d{12}$"))
        //    {
        //        printMode = Const.PRINT_MODE_TWO_SHORT;
        //    }
        //    else if ((startCode.StartsWith("LB") || startCode.StartsWith("RB")) && (startCode.Length == 11 || startCode.Length == 12))
        //    {
        //        printMode = Const.PRINT_MODE_TWO_SHORT;
        //        hasChar = true;
        //    }
        //    else if (Regex.IsMatch(startCode, @"\d{17}"))
        //    {
        //        printMode = Const.PRINT_MODE_ONE;
        //    }
        //    else if (startCode.StartsWith("Y") && startCode.Length == 13)
        //    {
        //        printMode = Const.PRINT_MODE_TWO_SHORT;
        //        hasChar = true;
        //    }
        //    else if (Regex.IsMatch(startCode, @"\d{16}"))
        //    {
        //        printMode = Const.PRINT_MODE_TWO_LONG;
        //    }
        //    else if (startCode.StartsWith("YX") && startCode.Length == 10)
        //    {
        //        printMode = Const.PRINT_MODE_ONE;
        //    }
        //    else
        //    {
        //        printMode = Const.PRINT_MODE_TWO_SHORT;
        //    }


        //    if (printMode == Const.PRINT_MODE_ONE)
        //    {
        //        for (int i = 0; i < totalCnt; i++)
        //        {
        //            PrintInfo printInfo = new PrintInfo();
        //            printInfo.strCode = new string[] { lstPrintInfo[i].leaveBH };
        //            if (codeLength == 17)
        //            {
        //                printInfo.narrow = "4";
        //                printInfo.wide = "4";
        //                LackCodePrint.PrtBarCode1AS17(printInfo);
        //            }
        //            else
        //            {
        //                printInfo.narrow = "5";
        //                printInfo.wide = "5";
        //                LackCodePrint.PrtBarCode1AS10(printInfo);
        //            }
        //        }

        //    }
        //    else
        //    {
        //        if (printMode == Const.PRINT_MODE_TWO_LONG)
        //        {
        //            PrtBarCode2L();
        //        }
        //        else
        //        {
        //            if (hasChar)
        //            {
        //                PrtBarCode2SWithChar();
        //            }
        //            else
        //            {
        //                PrtBarCode2S();
        //            }
        //        }
        //    }
        //}

        //private void PrtBarCode2S()
        //{
        //    for (int i = 0; i < totalCnt; i = i + 2)
        //    {
        //        string rightCode = "";
        //        if (i < totalCnt - 1)
        //        {
        //            rightCode = lstPrintInfo[i + 1].leaveBH;
        //        }
        //        PrintInfo printInfo = new PrintInfo();
        //        printInfo.strCode = new string[] { lstPrintInfo[i].leaveBH, rightCode };
        //        LackCodePrint.PrtBarCode2S(printInfo);
        //    }
        //}

        //private void PrtBarCode2SWithChar()
        //{
        //    for (int i = 0; i < totalCnt; i = i + 2)
        //    {
        //        string rightCode = "";
        //        if (i < totalCnt - 1)
        //        {
        //            rightCode = lstPrintInfo[i + 1].leaveBH;
        //        }
        //        PrintInfo printInfo = new PrintInfo();
        //        printInfo.strCode = new string[] { lstPrintInfo[i].leaveBH, rightCode };
        //        printInfo.narrow = "2";
        //        printInfo.wide = "2";
        //        LackCodePrint.PrtBarCode2SWithChar(printInfo);
        //    }
        //}

        //private void PrtBarCode2L()
        //{
        //    for (int i = 0; i < totalCnt; i = i + 2)
        //    {
        //        string rightCode = "";
        //        if (i < totalCnt - 1)
        //        {
        //            rightCode = lstPrintInfo[i + 1].leaveBH;
        //        }
        //        PrintInfo printInfo = new PrintInfo();
        //        printInfo.strCode = new string[] { lstPrintInfo[i].leaveBH, rightCode };
        //        printInfo.narrow = "4";
        //        printInfo.wide = "4";
        //        LackCodePrint.PrtBarCode2L(printInfo);
        //    }
        //}





    }
}
